Note: This tutorial assumes that you have completed the previous tutorials: Building a Visual Robot Model with URDF from Scratch, Building a Movable Robot Model with URDF, Adding Physical and Collision Properties to a URDF Model.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Using Xacro to Clean Up a URDF File

Description: Learn some tricks to reduce the amount of code in a URDF file using Xacro

Keywords: URDF, Xacro

Tutorial Level: BEGINNER

By now, if you’re following all these steps at home with your own robot design, you might be sick of doing all sorts of math to get very simple robot descriptions to parse correctly. Fortunately, you can use the xacro package to make your life simpler. It does three things that are very helpful.

  • Constants
  • Simple Math
  • Macros

In this tutorial, we take a look at all these shortcuts to help reduce the overall size of the URDF file and make it easier to read and maintain.

Using Xacro

As its name implies, xacro is a macro language for XML. The xacro program runs all of the macros and outputs the result. Typical usage looks something like this:

 xacro --inorder model.xacro > model.urdf 

On ROS distros melodic and later, you should omit the {--inorder} argument.

You can also automatically generate the urdf in a launch file. This is convenient because it stays up to date and doesn’t use up hard drive space. However, it does take time to generate, so be aware that your launch file might take longer to start up. (I’m looking at you pr2_description)

   1 <param name="robot_description"
   2   command="xacro --inorder '$(find pr2_description)/robots/pr2.urdf.xacro'" />

At the top of the URDF file, you must specify a namespace in order for the file to parse properly. For example, these are the first two lines of a valid xacro file:

   1 <?xml version="1.0"?>
   2 <robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="firefighter">

Constants

Let’s take a quick look at our base_link in R2D2.

   1   <link name="base_link">
   2     <visual>
   3       <geometry>
   4         <cylinder length="0.6" radius="0.2"/>
   5       </geometry>
   6       <material name="blue"/>
   7     </visual>
   8     <collision>
   9       <geometry>
  10         <cylinder length="0.6" radius="0.2"/>
  11       </geometry>
  12     </collision>
  13   </link>

The information here is a little redundant. We specify the length and radius of the cylinder twice. Worse, if we want to change that, we need to do so in two different places.

Fortunately, xacro allows you to specify properties which act as constants. Instead, of the above code, we can write this.

   1 <xacro:property name="width" value="0.2" />
   2 <xacro:property name="bodylen" value="0.6" />
   3 <link name="base_link">
   4     <visual>
   5         <geometry>
   6             <cylinder radius="${width}" length="${bodylen}"/>
   7         </geometry>
   8         <material name="blue"/>
   9     </visual>
  10     <collision>
  11         <geometry>
  12             <cylinder radius="${width}" length="${bodylen}"/>
  13         </geometry>
  14     </collision>
  15 </link>
  • The two values are specified in the first two lines. They can be defined just about anywhere (assuming valid XML), at any level, before or after they are used. Usually they go at the top.
  • Instead of specifying the actual radius in the geometry element, we use a dollar sign and curly brackets to signify the value.
  • This code will generate the same code shown above.

The value of the contents of the ${} construct are then used to replace the ${}. This means you can combine it with other text in the attribute.

   1 <xacro:property name=”robotname” value=”marvin” />
   2 <link name=”${robotname}s_leg” />

This will generate

   1 <link name=”marvins_leg” />

However, the contents in the ${} don’t have to only be a property, which brings us to our next point...

Math

You can build up arbitrarily complex expressions in the ${} construct using the four basic operations (+,-,*,/), the unary minus, and parenthesis. Examples:

   1 <cylinder radius="${wheeldiam/2}" length="0.1"/>
   2 <origin xyz="${reflect*(width+.02)} 0 0.25" />

All of the math is done using floats, hence

   1 <link name="${5/6}"/>

evaluates to

   1 <link name="0.833333333333"/>

In Jade and later distros, you can use more than the basic operations listed above, notably sin and cos.

Macros

Here’s the biggest and most useful component to the xacro package.

Simple Macro

Let’s take a look at a simple useless macro.

   1 <xacro:macro name="default_origin">
   2     <origin xyz="0 0 0" rpy="0 0 0"/>
   3 </xacro:macro>
   4 <xacro:default_origin />

(This is useless, since if the origin is not specified, it has the same value as this.) This code will generate the following.

   1 <origin rpy="0 0 0" xyz="0 0 0"/>
  • The name is not technically a required element, but you need to specify it to be able to use it.
  • Every instance of the <xacro:$NAME /> is replaced with the contents of the xacro:macro tag.

  • Note that even though its not exactly the same (the two attributes have switched order), the generated XML is equivalent.
  • If the xacro with a specified name is not found, it will not be expanded and will NOT generate an error.

Parameterized Macro

You can also parameterize macros so that they don’t generate the same exact text every time. When combined with the math functionality, this is even more powerful.

First, let’s take an example of a simple macro used in R2D2.

   1     <xacro:macro name="default_inertial" params="mass">
   2         <inertial>
   3                 <mass value="${mass}" />
   4                 <inertia ixx="1.0" ixy="0.0" ixz="0.0"
   5                      iyy="1.0" iyz="0.0"
   6                      izz="1.0" />
   7         </inertial>
   8     </xacro:macro>

This can be used with the code

   1 <xacro:default_inertial mass="10"/>

The parameters act just like properties, and you can use them in expressions

You can also use entire blocks as parameters too.

   1 <xacro:macro name="blue_shape" params="name *shape">
   2     <link name="${name}">
   3         <visual>
   4             <geometry>
   5                 <xacro:insert_block name="shape" />
   6             </geometry>
   7             <material name="blue"/>
   8         </visual>
   9         <collision>
  10             <geometry>
  11                 <xacro:insert_block name="shape" />
  12             </geometry>
  13         </collision>
  14     </link>
  15 </xacro:macro>
  16 
  17 <xacro:blue_shape name="base_link">
  18     <cylinder radius=".42" length=".01" />
  19 </xacro:blue_shape>
  • To specify a block parameter, include an asterisk before its parameter name.
  • A block can be inserted using the insert_block command
  • Insert the block as many times as you wish.

Practical Usage

The xacro language is rather flexible in what it allows you to do. Here are a few useful ways that xacro is used in the R2D2 model, in addition to the default inertial macro shown above.

To see the model generated by a xacro file, run the same command as with previous tutorials: roslaunch urdf_tutorial display.launch model:=urdf/08-macroed.urdf.xacro

(The launch file has been running the xacro command this whole time, but since there were no macros to expand, it didn't matter)

Leg macro

Often you want to create multiple similar looking objects in different locations. Often, there will be some symmetry to the locations. You can use a macro and some simple math to reduce the amount of code you have to write, like we do with R2’s two legs.

   1 <xacro:macro name="leg" params="prefix reflect">
   2     <link name="${prefix}_leg">
   3         <visual>
   4             <geometry>
   5                 <box size="${leglen} 0.1 0.2"/>
   6             </geometry>
   7             <origin xyz="0 0 -${leglen/2}" rpy="0 ${pi/2} 0"/>
   8             <material name="white"/>
   9         </visual>
  10         <collision>
  11             <geometry>
  12                 <box size="${leglen} 0.1 0.2"/>
  13             </geometry>
  14             <origin xyz="0 0 -${leglen/2}" rpy="0 ${pi/2} 0"/>
  15         </collision>
  16         <xacro:default_inertial mass="10"/>
  17     </link>
  18 
  19     <joint name="base_to_${prefix}_leg" type="fixed">
  20         <parent link="base_link"/>
  21         <child link="${prefix}_leg"/>
  22         <origin xyz="0 ${reflect*(width+.02)} 0.25" />
  23     </joint>
  24     <!-- A bunch of stuff cut -->
  25 </xacro:macro>
  26 <xacro:leg prefix="right" reflect="1" />
  27 <xacro:leg prefix="left" reflect="-1" />
  • Common Trick 1: Use a name prefix to get two similarly named objects.
  • Common Trick 2: Use math to calculate joint origins. In the case that you change the size of your robot, changing a property with some math to calculate the joint offset will save a lot of trouble.
  • Common Trick 3: Using a reflect parameter, and setting it to 1 or -1. See how we use the reflect parameter to put the legs on either side of the body in the base_to_${prefix}_leg origin.

Other tricks

Feel free to append your own tricks here.

Next Steps

This concludes this section however if you have completed all these steps you are well prepared to move into simulation. Continue on to Using a URDF in Gazebo

Wiki: urdf/Tutorials/Using Xacro to Clean Up a URDF File (last edited 2021-10-07 18:43:13 by DavidLu)